/**
 * 
 */
package tests;

import java.util.Vector;

import junit.framework.TestCase;
import objects.Frm;
import objects.Message;
import objects.Trd;
import objects.User;
import DB.DB;
import Domain.forumManager.ForumController;
import Domain.forumProtcol.FourmProtocol;
import Domain.stompProtocol.ForumFrame;
import Domain.stompProtocol.StompClient;
import Domain.stompProtocol.StompFrame;

/**
 * @author Asaf
 *
 */
public class FourmProtocolTest extends TestCase {

	/**
	 * Test method for {@link Domain.forumProtcol.FourmProtocol#getNameOfUser(java.lang.String)}.
	 */
	DB _database=new DB();
	ForumController f=new ForumController(_database);
	StompClient stompClient = new StompClient("127.0.0.1",80);
	FourmProtocol protocol = new FourmProtocol(stompClient,f);
	

	public void testGetNameOfUser() {
		
			String str="add message ; Manassen ; thread ; msg";
			assertEquals(protocol.getNameOfForum(str),"Manassen");
			assertEquals(protocol.getNameOfThread(str),"thread");
			assertEquals(protocol.getContentsOfMessage(str),"msg");
			assertEquals(str, "add message ; Manassen ; thread ; msg");
	}


	
	/**
	 * Test the following commands
	 * protocol :

			
			*add user 
			*add manager
			*remove user 
			*add message  ; [name of forum] ; [name of thread ] ; [message]
			*remove message  ; [name of forum] ; [name of thread ] ; [message] 
			*add thread  ; [name of forum] ; [name of thread ] 
			*add forum  ; [name of forum] 
			*get messages  ; [name of forum] ; [name of thread ] 
			*get threads ; [name of forum] 
			*get forums 
			*add friend  ; [name of user to be added]
			*remove friend  ; [name of user to be removed]
			*change type to manager 
			*change type to default user 
			*get users
			*get type of user

	 */
	public void testProcessMessage() {
		String nameOfUser="Asaf";
		
		//test add user
		
		StompFrame s=StompFrame.createSendFrame("Asaf","Asaf" , "add user");
		String ans=protocol.processMessage(s);
		//System.out.println(ans);
		
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.TYPESECCESS);
		assertEquals(this._database.getUsers().get(1).get_nameOfUser(), "Asaf");
	
		
		//test add manager  ,  get type of user
		s=StompFrame.createSendFrame("Inbal","Inbal" , "add manager");
		ans=protocol.processMessage(s);
		
		
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.TYPESECCESS);
		assertEquals(this._database.getUsers().get(2).get_nameOfUser(), "Inbal");
		
		s=StompFrame.createSendFrame("Inbal","Inbal" , "get type of user");
		ans=protocol.processMessage(s);
		assertEquals(protocol.getForumfrm().getType(), ForumFrame.TYPEManager);
		
		//test add forum and change type to manager
		
			//try to open forum as a default user
		s=StompFrame.createSendFrame("Asaf","Asaf" , "add forum ; Asaf; Cooking");
		ans=protocol.processMessage(s);
		//System.out.println(ans);
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.ERRInvalidTypeOfUSer);
				
			//try to become a manager
		s=StompFrame.createSendFrame("Asaf","Asaf" , "change type to manager ;Asaf");
		ans=protocol.processMessage(s);
		System.out.println(ans);
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.TYPESECCESS);
		
			//try to open forum as a manager
		s=StompFrame.createSendFrame("Asaf","Asaf" , "add forum ; Cooking");
		ans=protocol.processMessage(s);
		//System.out.println(ans);
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.TYPESECCESS);
		assertEquals(this._database.get_forum().get(0).getNameOfFourm(),"Cooking");
		
		//test add thread
			//add default user
		s=StompFrame.createSendFrame("Dudi","Dudi" , "add user");
		
		protocol.processMessage(s);
		String nameOfNotMangerUser="Dudi";

		//default user try to add thread
		s=StompFrame.createSendFrame("Dudi","Dudi" , "add thread ; Cooking ; a");
		ans=protocol.processMessage(s);
		System.out.println(ans  + "this one!!!");
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.ERRInvalidTypeOfUSer);
			//manager try to add thread
		
		s=StompFrame.createSendFrame("Asaf","Asaf" , "add thread ; Cooking ; a");
		ans=protocol.processMessage(s);
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.TYPESECCESS);
		assertEquals(_database.getThreads("Cooking").get(0).get_nameOfThread(), "a");
		
		//test add message
		s=StompFrame.createSendFrame("Asaf","Asaf" , "add message ; Cooking ; a ; msg");
		ans=protocol.processMessage(s);
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.TYPESECCESS);
		assertEquals(_database.getMessageFromThread("Cooking", "a").get(0).getMessage(), "msg");
		//assertEquals(ans,nameOfUser + " publish a new message ,in thread a\n"
			//	+"in fourm Cooking\n");
		

		//test get Forums
		s=StompFrame.createSendFrame("Asaf","Asaf" , "get forums ");
		
		ans=protocol.processMessage(s);
		
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.VECTORFRM);
		Vector<Frm> fff =(Vector<Frm>)protocol.getForumfrm().getData();
		assertEquals(fff.get(0).getNameOfFourm(), "Cooking");
		
		
		//test get Threads
		
		s=StompFrame.createSendFrame("Asaf","Asaf" , "get threads  ; Cooking");
		
		ans=protocol.processMessage(s);
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.VECTORTRD);
		Vector<Trd> ttt =(Vector<Trd>)protocol.getForumfrm().getData();
		assertEquals(ttt.get(0).get_nameOfThread(), "a");	
		
		
		//test get messages
		
		s=StompFrame.createSendFrame("Asaf","Asaf" , "get messages  ; Cooking ; a");
		//get messages [name of user] ; [name of forum] ; [name of thread ]
		ans=protocol.processMessage(s);
		
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.VECTORMSG);
		Vector<Message> mm =(Vector<Message>)protocol.getForumfrm().getData();
		assertEquals(mm.get(0).getMessage(), "msg");	
		
		//test remove message 
		
		s=StompFrame.createSendFrame("Asaf","Asaf" , "remove message ; Cooking ; a ; msg");
		ans=protocol.processMessage(s);
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.TYPESECCESS);
		
		assertEquals(_database.getMessageFromThread("Cooking", "a").size(), 0);
		
		
		//add friend  ; [name of user to be added]
		
		s=StompFrame.createSendFrame("Asaf","Asaf" , "add friend ; Inbal");
		ans=protocol.processMessage(s);
		assertTrue(_database.findUser("Inbal").getFrindList().contains(_database.findUser("Asaf")));
		//get friend get friends of user
		
		
		s=StompFrame.createSendFrame("Asaf","Asaf" , "get friends of user");
		ans=protocol.processMessage(s);
		Vector<User> uu=(Vector <User>)protocol.getForumfrm().getData();
		assertEquals(uu.get(0).get_nameOfUser(), "Inbal");
		
		
		
		//remove friend  ; [name of user to be removed]
		s=StompFrame.createSendFrame("Asaf","Asaf" , "remove friend ; Inbal");
		ans=protocol.processMessage(s);
		assertFalse(_database.findUser("Inbal").getFrindList().contains(_database.findUser("Asaf")));
		
		
		//      			change type to manager 
		s=StompFrame.createSendFrame("Dudi","Dudi" , "change type to manager");
		ans=protocol.processMessage(s);
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.TYPESECCESS);
		s=StompFrame.createSendFrame("Dudi","Dudi" , "get type of user");
		ans=protocol.processMessage(s);
		assertEquals(protocol.getForumfrm().getType(), ForumFrame.TYPEManager);
		//      			change type to default user 
		
		s=StompFrame.createSendFrame("Asaf","Asaf" , "change type to default user");
		ans=protocol.processMessage(s);
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.TYPESECCESS);
		s=StompFrame.createSendFrame("Asaf","Asaf" , "get type of user");
		ans=protocol.processMessage(s);
		assertEquals(protocol.getForumfrm().getType(), ForumFrame.TYPEDefaultUser);
		//      			get users
		s=StompFrame.createSendFrame("Asaf","Asaf" , "get users");
		ans=protocol.processMessage(s);
		Vector<User> u=(Vector<User>)protocol.getForumfrm().getData();
		assertEquals(u.size(), 4);
		assertEquals(this._database.getUsers().size(), 4);
		
		
		//test remove user
		s=StompFrame.createSendFrame("Asaf","Asaf" , "remove user");
		ans=protocol.processMessage(s);
		System.out.println(ans);
		assertEquals(protocol.getForumfrm().getType(),ForumFrame.TYPESECCESS);
		
		assertEquals(this._database.getUsers().size(), 3);
		
	}

}
